package com.autobet.services.aop;

import com.autobet.services.ResultService;
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by yangd on 2017-08-27.
 */
@Aspect
@Component
public class ExceptionInterceptor {

    private static final Logger log = LoggerFactory.getLogger(ExceptionInterceptor.class);
    @Autowired
    private ResultService resultService;

    @Pointcut("execution(* com.autoleo..*(..))")
    public void exceptionMethodPointcut() {}

    @Around("exceptionMethodPointcut()")
    public Object Interceptor(ProceedingJoinPoint pjp)
    {
        long beginTime = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature)pjp.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();

        Object result = null;
        try
        {
            result = pjp.proceed();
        }
        catch (Throwable e)
        {
            this.resultService.addInfo("[系统预警]" + e.getMessage(), "02");
        }
        long costMs = System.currentTimeMillis() - beginTime;
        log.debug("方法{}执行时间{}", methodName, Long.valueOf(costMs));

        return result;
    }

}
